/**
 * Project Name:springBoot-serviceImpl
 * File Name:IoServiceImp.java
 * Package Name:com.libra.io.serviceImp
 * Date:2018年8月28日上午10:47:25
 * Copyright (c) 2018, wangzhch All Rights Reserved.
 *
*/

package com.libra.io.serviceImp;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.libra.io.service.IoService;

/**
 * ClassName:文件上传下载实现层<br/>
 * Date: 2018年8月28日 上午10:47:25 <br/>
 * 
 * @author wangzhch
 * @version
 * @since JDK 1.8
 * @see
 */
@Service
public class IoServiceImp implements IoService {
	static Logger log = LoggerFactory.getLogger(IoServiceImp.class);
	/***
	 * 
	 * 上传到项目的跟路径
	 * 
	 * @throws IOException
	 * @see com.libra.io.service.IoService#testUploadByHellPath(org.springframework.web.multipart.MultipartFile)
	 */
	@Override
	public String testUploadByHellPath(MultipartFile file) throws IOException{
		log.info("testUploadByHellPath");
		if (!file.isEmpty()) {
			/*
			 * 这段代码执行完毕之后，图片上传到了工程的跟路径； 大家自己扩散下思维，如果我们想把图片上传到 d:/files大家是否能实现呢？ 等等;
			 * 这里只是简单一个例子,请自行参考，融入到实际中可能需要大家自己做一些思考，比如： 1、文件路径； 2、文件名； 3、文件格式; 4、文件大小的限制;
			 */
			BufferedOutputStream out = new BufferedOutputStream(
					new FileOutputStream(new File(file.getOriginalFilename())));
			out.write(file.getBytes());
			out.flush();
			out.close();
			return file.getOriginalFilename()+"上传成功";
		} else {
			return "上传失败，因为文件是空的.";
		}
	}
	/**
	 * 
	 * 文件下载.
	 * @return 
	 * @see com.libra.io.service.IoService#testDownload()
	 */
	@Override
	public String testDownload(HttpServletResponse response,String fileName) {
		Long startTime = System.currentTimeMillis();
		response.setHeader("content-type", "application/octet-stream");
		response.setContentType("application/octet-stream");
		try {
			response.setHeader("Content-Disposition", "attachment;filename=" + java.net.URLEncoder.encode(fileName, "UTF-8"));
		} catch (UnsupportedEncodingException e2) {
			e2.printStackTrace();
		}
		byte[] buff = new byte[1024];
		BufferedInputStream bis = null;
		OutputStream os = null;		
		try {
			String path = System.getProperty("user.dir");//获取work工作空间的根路径
			log.info("文件路径:{}",path +File.separator+ fileName);
			os = response.getOutputStream();
			bis = new BufferedInputStream(new FileInputStream(new File(path +File.separator+ fileName)));
			int i = bis.read(buff);
			while (i != -1) {
				os.write(buff, 0, buff.length);
				os.flush();
				i = bis.read(buff);
			}
		} catch (FileNotFoundException e1) {
			return "系统找不到指定的文件";
		}catch (IOException e) {
			log.info("异常:{}",e.getMessage());
			return e.getMessage();
		} finally {
			if (bis != null) {
				try {
					bis.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
		Long endTime = System.currentTimeMillis();
		return String.valueOf(endTime-startTime);
	}
}
